package cn.com.b2office.dao.hibernate;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import cn.com.b2office.dao.CountyDao;
import cn.com.b2office.domain.City;
import cn.com.b2office.domain.County;
import cn.com.b2office.domain.District;

public class CountyDaoHibernateImpl extends HibernateDaoSupport implements
		CountyDao {

	public List<City> findAllCities() {
		List<City> list = null;
		Session s = getSession();
		try {
			Query query = s.createQuery("from City");
			query.setCacheable(true);
			list = query.list();
		} catch (RuntimeException e) {
			throw e;
		} finally {
			releaseSession(s);
		}
		return list;
	}

	public List<County> findAllCounties() {
		List<County> list = null;
		Session s = getSession();
		try {
			Query query = s.createQuery("from County");
			query.setCacheable(true);
			list = query.list();
		} catch (RuntimeException e) {
			throw e;
		} finally {
			releaseSession(s);
		}
		return list;
	}

	public List<District> findAllDistricts() {
		List<District> list = null;
		Session s = getSession();
		try {
			Query query = s.createQuery("from District");
			query.setCacheable(true);
			list = query.list();
		} catch (RuntimeException e) {
			throw e;
		} finally {
			releaseSession(s);
		}
		return list;
	}

	public City getCity(int cityId) {
		return (City) getHibernateTemplate().get(City.class, cityId);
	}

	public County getCounty(int countyId) {
		return (County) getHibernateTemplate().get(County.class, countyId);
	}

	public District getDistrict(int districtId) {
		return (District) getHibernateTemplate()
				.get(District.class, districtId);
	}

}
